<template>
  <div class="register-container retrieve-pwd">
    <el-form ref="dataForm" :model="postForm" :rules="registerRules" class="register-form" autocomplete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">忘记密码-验证手机号</h3>
      </div>
      <el-form-item prop="mobile">
        <span class="svg-container">
          <i class="el-icon-mobile-phone" />
        </span>
        <el-input
          id="mobile"
          ref="mobile"
          v-model="postForm.mobile"
          placeholder="手机号"
          name="mobile"
          type="text"
          tabindex="1"
          autocomplete="off"
          maxlength="11"
        />
<!--        <span :class="[sendStatus === 1 ?'show-pwd': 'show-pwd-default']" id="sendMessage" @click="sendStatus === 1 && getMessage()">-->
<!--          <svg-icon icon-class="guide" />-->
<!--        </span>-->
<!--        <span id="showCount" style="color: white; display: none">{{ countt }}s</span>-->
        <span class="show-pwd-default" id="sendMessage">
          <el-button :disabled="sendStatus === 0" type="primary"  @click="getMessage()"><span id="btnText">获取验证码</span></el-button>
          <span id="showCount" class="show-pwd-default"  style="color: white; display: none;">{{ countt }}s</span>
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <i class="el-icon-message" />
        </span>
        <el-input
          ref="code"
          v-model="postForm.code"
          placeholder="验证码"
          name="code"
          type="text"
          tabindex="1"
          autocomplete="off"
          maxlength="4"
          @keyup.native="changeCircle"
        />
        <span class="show-pwd-default" v-if="circleFlag">
          <!--<el-button :type="circleType" :icon="circleIcon" circle></el-button>-->
          <i :style="circleType" :class="circleIcon"></i>
        </span>
      </el-form-item>
      <el-button ref="submitButton" type="primary" :disabled="btnStatus === 0" style="margin-bottom:30px; float: right" @click.native.prevent="submitForm">下一步</el-button>
    </el-form>
  </div>
</template>

<script>
import { validMobile } from '@/utils/validate'
import { validateMobile, getValidateCode, retrieveOne } from '@/api/system/user'
import { checkMoibleCode } from '@/api/register'

export default {
  name: 'RetrieveOne',
  data() {
    const validateMobileVal = (rule, value, callback) => {
      if (!validMobile(value)) {
        this.btnStatus = 0
        this.sendStatus = 0
        callback(new Error('手机号码格式不正确'))
      } else {
        this.checkMobile(value, callback)
        this.validMobileFlag = true
        // this.btnStatus = 1
        // this.sendStatus = 1
        // callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value === null || value.trim() === '') {
        this.btnStatus = 0
        callback(new Error('验证码不能为空'))
      } else {
        if(this.validMobileFlag && this.getCodeFlag){ //手机号校验通过并发送验证码
          this.btnStatus = 1
        }
        callback()
      }
    }
    return {
      sendStatus: 0,
      btnStatus: 0,
      timer: '',
      countt: 60,
      validMobileFlag: false,
      getCodeFlag: false,
      circleFlag: false,
      circleType: '',
      circleIcon: '',
      postForm: {
        mobile: '',
        code: ''
      },
      registerRules: {
        mobile: [{ required: true, trigger: 'change', validator: validateMobileVal }],
        code: [{ required: true, trigger: 'change', validator: validateCode }]
      }
    }
  },
  mounted() {

  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    checkMobile(mobile, callback) {
      validateMobile({ mobile: mobile }).then(response => {
        const returnCode = response && response.returnCode
        if (returnCode == this.Const.OK) {
          this.sendStatus = 0
          callback(new Error("手机号不存在"))
        } else {
          this.sendStatus = 1
          callback()
        }
      })
    },
    getMessage() {
      const value = document.getElementById('mobile').value
      if (validMobile(value)) {
        getValidateCode({ mobile: value, type: '2' }).then(response => {
          if (this.Const.OK === response.returnCode) {
            this.$message({
              type: 'success',
              message: '获取验证码成功'
            })
            this.timer = setInterval(this.setTime, 1000)
            this.showCount()
            this.getCodeFlag = true;
          } else {
            this.$message({
              type: 'error',
              message: response && response.msg || '验证失败'
            })
            this.showSend()
          }
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '获取验证码失败'
          })
          this.showSend()
        })
      }
    },
    changeCircle() {
      checkMoibleCode({ mobile: this.postForm.mobile, code: this.postForm.code }).then(response => {
        if (this.Const.OK === response.returnCode) {
          this.circleType = 'color:#67c23A'
          this.circleIcon = 'el-icon-check'
          this.circleFlag = true
        } else {
          this.circleType = 'color:#F56C6C'
          this.circleIcon = 'el-icon-close'
          this.circleFlag = true
        }
      }).catch(() => {
        this.circleType = 'color:#F56C6C'
        this.circleIcon = 'el-icon-close'
        this.circleFlag = true
      })
    },
    setTime() {
      this.countt --
      if (this.countt < 1) {
        this.countt = 60
        this.showSend()
      } else {
        this.showCount()
      }
    },
    showSend() {
      document.getElementById('btnText').innerHTML = '获取验证码'
      this.sendStatus = 1
      clearInterval(this.timer)
    },
    showCount() {
      document.getElementById('btnText').innerHTML = this.countt + 's'
      this.sendStatus = 0
    },
    submitForm() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          retrieveOne(this.postForm).then(response => {
            const returnCode = response && response.returnCode
            if (this.Const.OK === returnCode) {
              this.$message({
                type: 'success',
                message: '验证成功'
              })
              clearInterval(this.timer)
              this.$router.push({ path: '/retrieve-two', query:{ mobile: this.postForm.mobile }})
            } else {
              this.$message({
                type: 'error',
                message: response && response.msg || '验证失败'
              })
            }
          }).catch(() => {
            this.$message({
              type: 'error',
              message: '验证失败'
            })
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .retrieve-pwd .el-input input {
    color: $bg;
  }
}

/* reset element-ui css */
.retrieve-pwd {
  background: url(../../assets/images/register.jpg) no-repeat;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray:#889aa4;
$light_gray:#eee;

.retrieve-pwd {
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    color: $light_gray;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $light_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $light_gray;
    cursor: pointer;
    user-select: none;
  }

  .show-pwd-default {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
